package org.mosquitos.chat;

//import java.io.IOException;

import java.io.IOException;

import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.peergroup.PeerGroup;

public class AdvertisementPublishService implements Runnable {
	private PeerGroup peerGroup;
	private Advertisement advertisement;
	
	public AdvertisementPublishService(PeerGroup peerGroup,Advertisement advertisement){
		this.peerGroup = peerGroup;
		this.advertisement = advertisement;
	}

	public void run() {
		DiscoveryService discoveryService = peerGroup.getDiscoveryService();
		/*
		try {
			discoveryService.publish(advertisement);
		} catch (IOException e) {
			e.printStackTrace();
		}
		*/
		
		while(true){
			try {
				discoveryService.publish(advertisement);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			discoveryService.remotePublish(advertisement);
			
	        synchronized (this){
		        try {
					this.wait(2*1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
		}
	}
}
